package com.example.administrator.printoutindemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.dds.soap.SoapListener;
import com.dds.soap.SoapParams;
import com.example.administrator.printoutindemo.base.BaseActivity;
import com.example.administrator.printoutindemo.http.SPUtil;
import com.example.administrator.printoutindemo.zxing.activity.CaptureActivity;
import com.jess.arms.base.BaseConstants;
import com.jess.arms.utils.ArmsUtils;

import org.ksoap2.SoapFault;
import org.ksoap2.serialization.SoapObject;

public class ZhiliangshouyangActivity extends BaseActivity {
    private ImageView iv_qysaomiao;
    private TextView tv_quyangdizhi, tv_wuliaoname, tv_quyangcount, tv_danweiguige, tv_check;
    private String scanResult = "";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.zhiliangshouyangyuan);
        iv_qysaomiao = findViewById(R.id.iv_qysaomiao);
        tv_quyangdizhi = findViewById(R.id.tv_quyangdizhi);
        tv_wuliaoname = findViewById(R.id.tv_wuliaoname);
        tv_quyangcount = findViewById(R.id.tv_quyangcount);
        tv_danweiguige = findViewById(R.id.tv_danweiguige);
        tv_check = findViewById(R.id.tv_check);

        iv_qysaomiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent openCameraIntent = new Intent(ZhiliangshouyangActivity.this,
                        CaptureActivity.class);
                startActivityForResult(openCameraIntent, 0);
            }
        });

        tv_check.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                setSimpleReceiveSuccess();
            }
        });
    }

    private void setSimpleReceiveSuccess() {


        String methodName = "setSimpleReceiveSuccess";

        SoapParams params = new SoapParams();
        String chushi = SPUtil.getString(ZhiliangshouyangActivity.this, "quyangerweima", "");
        String jiequ = chushi.substring(0, chushi.indexOf("_"));
        params.put("monthid", jiequ);

        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (result.equals("success")) {
                        ArmsUtils.makeText(ZhiliangshouyangActivity.this, "收样成功");
                        tv_check.setText("收样成功");
                        tv_check.setClickable(false);
                    } else {
                        ArmsUtils.makeText(ZhiliangshouyangActivity.this, "收样失败");
                    }

                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
            }
        }, getAapiUserName(), getApiPwd());
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            scanResult = bundle.getString("result");
            if (scanResult != null | TextUtils.isEmpty(scanResult)) {
                getMaterialInfoForQualitySimpleReceiver();
            }
        }
    }


    private void getMaterialInfoForQualitySimpleReceiver() {


        String methodName = "getMaterialInfoForQualitySimpleReceiver";

        SoapParams params = new SoapParams();
        params.put("simpleCode", scanResult);


        /**
         * 请求
         */
        soapUtil.callEntry(getUrl(), BaseConstants.NAME_SPACE, methodName, params, new SoapListener() {
            @Override
            public void onSuccess(int statusCode, SoapObject object) {

                String result = object.getProperty(methodName + "Result").toString();
                if (result != null && !result.equals("[]") && !result.equals("anyType{}") && !result.equals("Error") && !result.equals("-100")) {
                    if (result.equals("success")) {
                        Log.e("dayin=====ss======", result);
                        if (result.equals("fail")) {
                            ArmsUtils.makeText(ZhiliangshouyangActivity.this, "此二维码不存在");
                        } else {
                            JSONObject jsonObject = JSON.parseObject(result);
                            String qydd = jsonObject.getString("simpleGetAddress");
                            String qysl = jsonObject.getString("spotnum");
                            String wlmc = jsonObject.getString("material");
                            String dwgg = jsonObject.getString("unit");
                            tv_danweiguige.setText(dwgg);
                            tv_quyangcount.setText(qysl);
                            tv_quyangdizhi.setText(qydd);
                            tv_wuliaoname.setText(wlmc);
                            SPUtil.putString(ZhiliangshouyangActivity.this, "mids-s", jsonObject.getString("mid"));
                        }
                    } else {
                        ArmsUtils.makeText(ZhiliangshouyangActivity.this, "收样失败");
                    }

                } else {

                }

            }

            @Override
            public void onFailure(int statusCode, String content, Throwable error) {
            }

            @Override
            public void onFailure(int statusCode, SoapFault fault) {
            }
        }, getAapiUserName(), getApiPwd());
    }
}
